#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"

/********************************************************************************************
    Demura  Flashê1?ü
		Demura	Flash2á3y
		Demura	FlashD′è?
		Demura	FlashD￡?é
		Demura	Flash OTP
		Demura	1|?ü?a??&1?±?
********************************************************************************************
********************************************************************************************/
//{0xfe,0x26,0xf3,0x02,0x18,(USB_Rx_Demura_Total_Num>>16),0x19,(USB_Rx_Demura_Total_Num>>8),0x1A,USB_Rx_Demura_Total_Num};

void GM8A0_RM69200_Enable_Flash_Control_and_SPI_Flash_En(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u16 tmp=0;
	u8 buffer1[3];	
	u8 RM69200_Enable_Flash_Control[38] = {0xfe,0x26,0x02,0x02,0x08,0x12,0x04,0xcf,0x10,0x4B,0x14,0x22,0x1E,0x21,0xfe,0x20,0x5f,0x01,0x09,0x13,0xfe,0x26,0xf4,0x55,0xfe,0x20,0x5f,0x00,0xfe,0x26,0xf3,0x08,0xf3,0x00,0xf3,0x04,0xfe,0x26};	
		
	//u8 RM69200_Enable_Flash_Control[22] = {	0xfe,0x20,0x5f,0x00,0xfe,0x26,0x02,0x02,0x08,0x12,0x04,0xcf,0xf3,0x08,0xf3,0x00,0xf3,0x04,0xf3,0x02,0xfe,0x26};
	u8 RM69200_Enable_Flash_Read_REG[3] = {0xEA,0xEB,0xEC};	
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	for(i=0;i<sizeof(RM69200_Enable_Flash_Control);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM69200_Enable_Flash_Control[i];
		buffer1[2] = RM69200_Enable_Flash_Control[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(32);
		i+=1;
	}
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
	delay_ms(15);
	Generate_MIPI_Read(pdev,SigMode,channel,RM69200_Enable_Flash_Read_REG[0] ,1, buffer+4);
	Generate_MIPI_Read(pdev,SigMode,channel,RM69200_Enable_Flash_Read_REG[1] ,1, buffer+5);
	Generate_MIPI_Read(pdev,SigMode,channel,RM69200_Enable_Flash_Read_REG[2] ,1, buffer+6);
	
	buffer[0] = 0x2F;
	buffer[1] = 0x06;		

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);        																					
}

void GM8A0_RM69200_Inter_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	//调不通的时候，记得末尾加00
	u8 RM69200_Inter_Demura_Ram_Cmd_Table[50]={0xfe,0xD2,0x19,0x20,0xfe,0xD0,0x49,0x03,0x07,0x01,0x42,0x81,0xfe,0x40,0x0e,0xc0,0xfe,0xd0,0x55,0x11,0xfe,0x26,0xf2,0x04,0x1b,0x00,0x36,0x80,0xfe,0xb0,0xf6,0x02,0xfe,0xb2,0xe3,0x40,0xfe,0xb2,0xe3,0x60,0xfe,0xb2,0xe3,0x40,0xfe,0xb2,0xe3,0x00,0xfe,0x00};		
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
		
	for(i=0;i<sizeof(RM69200_Inter_Demura_Ram_Cmd_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM69200_Inter_Demura_Ram_Cmd_Table[i];
		buffer1[2] = RM69200_Inter_Demura_Ram_Cmd_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(15);
		i+=1;
	}	
	buffer[0] = 0x2F;
	buffer[1] = 0x01;		

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);        																					
}

                                    
void GM8A0_RM69200_Exit_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}
                               
void GM8A0_RM69200_Flash_Write_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		u32 error_times=0;
		u8 buffer1[3];
		u8 RM69200_Flash_Write_Demura_data_Table[4]={0xFE,0x26,0xF2,0x10};
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);	
		
		for(i=0;i<sizeof(RM69200_Flash_Write_Demura_data_Table);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM69200_Flash_Write_Demura_data_Table[i];
			buffer1[2] = RM69200_Flash_Write_Demura_data_Table[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(15);
			i+=1;
		}	
		delay_ms(6000);
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
		SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package  
		do
		{       
				buffer1[0] = 0x01;
				buffer1[1] = 0xDB;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(6);
				buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);   
			delay_ms(100);
		}while((buffer[4]&0x80)!=0x80);
		delay_ms(5);
		buffer[0] = 0x2F;
		buffer[1] = 0x03;		
		buffer[4] = 0x80;				

//		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);		
}                       

void GM8A0_RM69200_Flash_Write_Demura2(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}


void GM8A0_RM69200_Flash_Check_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u16 tmp=0;
	u8 buffer1[3];
	u8 RM69200_Flash_Check_Read_REG[2]={0xE6,0xE7};
	u8 RM69200_Flash_Check_Demura_data_Table[28]={0xfe,0x20,0x5F,0x00,0xfe,0x26,0xf3,0x08,0xf3,0x00,0xf3,0x04,0xf2,0x04,0x15,0x00,0x16,0x00,0x17,0x00,0x18,0x09,0x19,0x99,0x1A,0x00,0xf2,0x01,};  
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);

		for(i=0;i<sizeof(RM69200_Flash_Check_Demura_data_Table);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM69200_Flash_Check_Demura_data_Table[i];
			buffer1[2] = RM69200_Flash_Check_Demura_data_Table[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(10);
			i+=1;
		}	
		delay_ms(2000);
	
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);		
    for( i = 0;i<sizeof(RM69200_Flash_Check_Read_REG);i++)			 	
    {  	
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package                           
        buffer1[0] = 0x01;
        buffer1[1] = RM69200_Flash_Check_Read_REG[i];
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(6);
        buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
    }			
	  delay_ms(5);
		buffer[0] = 0x2F;
		buffer[1] = 0x04;					
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  	
}
                                  
void GM8A0_RM69200_Flash_Erase_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	u8 RM69200_Flash_Erase_Demura_data_Table[22]={0xfe,0x26,0x15,0x00,0x16,0x00,0x17,0x00,0x18,0x09,0x19,0x99,0x1A,0x00,0xFE,0x26,0x1B,0x00,0xF2,0x04,0xF3,0x01};

	u8 RM69200_Read_REG=0xDB;	
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	
	for(i=0;i<sizeof(RM69200_Flash_Erase_Demura_data_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM69200_Flash_Erase_Demura_data_Table[i];
		buffer1[2] = RM69200_Flash_Erase_Demura_data_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}
	delay_ms(6000);
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); delay_ms(15);
	do
	{   
		Generate_MIPI_Read(pdev,SigMode,channel,RM69200_Read_REG ,1, buffer+4);
		delay_ms(6);     
	}while((buffer[4]&0x80)!=0x80);
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x05;		
	buffer[4] = 0x80;	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú			
}

void GM8A0_RM69200_Flash_Demura_OTP(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}

void GM8A0_RM69200_Demura_Function_ON(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];
	u8 RM69200_Demura_Function_ON_Table[4] = {0xfe, 0x22, 0x77, 0x00};	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);	
	
	for(i=0;i<sizeof(RM69200_Demura_Function_ON_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM69200_Demura_Function_ON_Table[i];
		buffer1[2] = RM69200_Demura_Function_ON_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x07;		
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú
}

void GM8A0_RM69200_Demura_Function_OFF(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];
	u8 RM69200_Demura_Function_OFF_Table[4] = {0xfe, 0x22, 0x77, 0x02};		
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);

	for(i=0;i<sizeof(RM69200_Demura_Function_OFF_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM69200_Demura_Function_OFF_Table[i];
		buffer1[2] = RM69200_Demura_Function_OFF_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}

	delay_ms(5);
	buffer[0] = 0x2F;  //Demura_type
	buffer[1] = 0x08;				
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}

void ProcessForDmuR0B( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      //写寄存器
					GM8A0_RM69200_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      //读寄存器
					GM8A0_RM69200_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      //读寄存器
					GM8A0_RM69200_Flash_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      //读寄存器
					GM8A0_RM69200_Flash_Check_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      //读寄存器
					GM8A0_RM69200_Flash_Erase_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      //check flash ID
					GM8A0_RM69200_Enable_Flash_Control_and_SPI_Flash_En(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x07:                                      //check flash ID
					GM8A0_RM69200_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x08:                                      //check flash ID
					GM8A0_RM69200_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			case 0x0A:
					GM8A0_RM69200_Flash_Write_Demura2(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			default:
				break;												
	}
}
//********************************************************************************************